package put.cs.two.to.tsp.calculators.implementation.greedy;

import put.cs.two.to.tsp.calculators.base.AbstractTSPSolutionCalculator;
import put.cs.two.to.tsp.models.NodeSet;
import put.cs.two.to.tsp.models.Point;
import put.cs.two.to.tsp.models.TSPSolution;

public class NearestNeighbourCalculator extends AbstractTSPSolutionCalculator {

    @Override
    public TSPSolution solve(NodeSet inputNodes, int startingIndex) {
        try {
            tmpInputNodes = (NodeSet) inputNodes.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
        TSPSolution solution = new TSPSolution();
        Point startingPoint = inputNodes.get(startingIndex);
        Point currentPoint = startingPoint;
        solution.addPoint(startingPoint);
        for (int i = 1; i < inputNodes.getDestinatedCycleLength(); i++) {
            tmpInputNodes.setUsed(currentPoint);
            Point nearestPoint = getNearestPoint(currentPoint);
            solution.addPoint(nearestPoint);
            currentPoint = nearestPoint;
        }
        System.out.println(solution);
        return solution;
    }
}
